package com.transitup.gui.server;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;

import com.transitup.gui.client.GreetingService;
import com.transitup.gui.client.IncrementableInteger;
import com.transitup.gui.client.Result;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;
import com.google.gwt.dev.util.log.Loggers;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
		GreetingService {
	private Logger logger = Logger.getLogger(GreetingServiceImpl.class.toString());
	public Result greetServer(String input,String from, String to, IncrementableInteger request) throws Exception{
		System.setProperty("http.proxyHost", "global.proxy.lucent.com");
		System.setProperty("http.proxyPort", "8000");
//		System.setProperty("http.proxyUser","lmidmani");
//		System.setProperty("http.proxyPassword","kil7mop!");
		Result result=null;
		try{
			Translate.setHttpReferrer("http://transitup.appspot.com/");
			result = new Result();
			result.setResult(Translate.execute(input, Language.fromString(from), Language.fromString(to)));
//	        	Thread.sleep(2000);
//	        	result.setResult("from "+from +" to "+to+" : "+input);
	    	result.setId(request);
		}
		catch(Exception e)
		{
			logger.severe(e.getMessage());
			throw e;
		}
		return result;
	}
	public List<List<String>> getAvailableLanguages() {
		List<String> langs = new ArrayList<String>();
		List<String> labels = new ArrayList<String>();
		for(Language lang: Language.values())
		{
			try{
				Translate.setHttpReferrer("http://transitup.appspot.com/");
				Translate.execute("Hello world!", Language.ENGLISH,lang );
//				Thread.sleep(100);
//				System.out.println(lang);
				langs.add(lang.toString());
				labels.add(lang.name().replace('_',' ').toLowerCase());
			}
			catch(Exception e)
			{
				logger.severe("language not supported "+lang.name());
			}
		}
		List<List<String>> result = new ArrayList<List<String>>();
		result.add(langs);
		result.add(labels);
		return result;
	}
}
